
//  TouristViewController.h
//  SouthChina
//
//  Created by creator_lq on 14/12/1.
//  Copyright (c) 2014年 科创. All rights reserved.
//
//
//  游客界面


#import "TouristViewController.h"
#import "TouristCell.h"
#import "SIAlertView.h"
#import "AddressBookLeftViewController.h"
#import "WebViewController.h"
#import "BillboardTableViewCell.h"
#import "LoginMainViewController.h"

#define CEEL_HEIGHT (60)

@interface TouristViewController () 
{
    UITableView *_tablView;
    UIView *_bottomView;
    
    UILabel *_firstNewLable;
    UIImageView *_firstNewImageView;
    
    NSMutableArray *_dateList;
    NSString *_fistWebUrl;
}

@end

@implementation TouristViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
     
    [self addTitleBarWithBackBtn:@"移动校园门户"];
     CGFloat orginHeight = self.view.frame.size.height- TABBARHEIGHT-TITLE_BAR_HEGIHT-(YNEW);
    _tablView = [[UITableView alloc] initWithFrame:CGRectMake(0, TITLE_BAR_HEGIHT+(YNEW), SCREEN_WIDTH, orginHeight) style:UITableViewStylePlain];
    _tablView.delegate = self;
    _tablView.dataSource = self;
  
    int firstNewHeight = 260;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, firstNewHeight)];
    
     _firstNewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, firstNewHeight-50)];
    
     _firstNewLable =[[UILabel alloc] initWithFrame:CGRectMake(0, firstNewHeight-45, SCREEN_WIDTH, 30)];
    
    UILabel *LableBK =[[UILabel alloc] initWithFrame:CGRectMake(10, firstNewHeight-5, SCREEN_WIDTH-10, 1)];
    
    LableBK.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1.0];;
    
    _firstNewLable.font = [UIFont systemFontOfSize:15.0];
    _firstNewLable.backgroundColor = [UIColor clearColor];
    _firstNewImageView.contentMode = UIViewContentModeScaleAspectFill;
    _firstNewImageView.clipsToBounds = YES;
  
    _tablView.tableHeaderView = headerView;
    _tablView.bounces = NO;
    
#ifdef __IPHONE_7_0
    if ([_tablView respondsToSelector:@selector(separatorInset)]) {
        [_tablView setSeparatorInset:UIEdgeInsetsZero];
    }
#endif
    
    [headerView addSubview:_firstNewImageView];
    [headerView addSubview:_firstNewLable];
    [headerView addSubview:LableBK];
    [self.view addSubview:_tablView];
    
    _tabbar = [[TabbarView alloc]initWithFrame:CGRectMake(0,  self.view.frame.size.height- TABBARHEIGHT, SCREEN_WIDTH, TABBARHEIGHT)];
    _tabbar.delegate = self;
    _tabbar.tabbar.tabBarDelegate = self;
    [_tabbar setCurrentTab:0];
    [self.view addSubview:_tabbar];
    [self reloadDate];
    
    UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fistNewsClick)];
    
    [headerView addGestureRecognizer:tapGesture];
    
    //导航栏上面的+号按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(265.0f, (YNEW)+3, 44.0f, 34.0f);
    [rightBtn addTarget:self action:@selector(showRightBtnView:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
    rightBtn.imageEdgeInsets = UIEdgeInsetsMake ( 5 ,  10 , 5 , 10 );
    
    [self.titlebar addSubview:rightBtn];
}


-(void)showRightBtnView:(id)sender
{
    NSLog(@"登陆");
    LoginMainViewController *loginmain =[[LoginMainViewController alloc] initWithNibName:nil bundle:nil];
    loginmain.TypeLoginMain = @"0";
    [self.navigationController pushViewController:loginmain animated:YES];
}


-(void)fistNewsClick{
    WebViewController * detail = [[WebViewController alloc] init];
    detail.webUrl = _fistWebUrl;
    [self.navigationController pushViewController:detail animated:NO];
}

-(void) reloadDate{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary *dict  = [[NSDictionary alloc] init];
    
    NSDictionary* cacheDate = [DE serverCall:@"queryGuestNews" params:dict callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            if (success) {
                _dateList  = [[NSMutableArray alloc] initWithArray:[data objectForKey:@"articles"]];
                if(_dateList!=nil && _dateList.count>0){

                    NSURL *url = [NSURL URLWithString: [_dateList[0] objectForKey:@"PicUrl"]];
                    [_firstNewImageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
                    _firstNewLable.text =[_dateList[0] objectForKey:@"Title"];
                    _fistWebUrl =[_dateList[0] objectForKey:@"Url"];
                    [_dateList removeObjectAtIndex:0];

                }
                [_tablView reloadData];
            
            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:desc delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        });
        
    }needCache:YES];
    
    if(cacheDate){
        _dateList  = [[NSMutableArray alloc] initWithArray:[cacheDate objectForKey:@"articles"]];
        if(_dateList!=nil && _dateList.count>0){
            
            NSURL *url = [NSURL URLWithString: [_dateList[0] objectForKey:@"PicUrl"]];
            [_firstNewImageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
            _firstNewLable.text =[_dateList[0] objectForKey:@"Title"];
            _fistWebUrl =[_dateList[0] objectForKey:@"Url"];
            [_dateList removeObjectAtIndex:0];
            
        }
        [_tablView reloadData];
    }

    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
     NSLog(@"更新更新");
    [_tabbar setCurrentTab:0];
 
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dateList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   BillboardTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"BillboardTableViewCell" owner:self options:nil] objectAtIndex:0];
    NSURL *url = [NSURL URLWithString: [_dateList[indexPath.row] objectForKey:@"PicUrl"]];
    [cell.image setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    cell.labContent.text=[ _dateList[indexPath.row] objectForKey:@"Title"];
    cell.labDigest.text =[ _dateList[indexPath.row] objectForKey:@"Description"];
    
    return  cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"section:%ld  indexpath:%ld",(long)indexPath.section,(long)indexPath.row);
    WebViewController * detail = [[WebViewController alloc] init];
    detail.webUrl = [_dateList[indexPath.row] objectForKey:@"Url"];
    [self.navigationController pushViewController:detail animated:NO];
    [tableView reloadData];
}

- (void)tabBar:(SYTabBar *)tabBar didSelectItem:(SYTabBarItem *)item
{
    if (tabBar.tag == MAINTABBAR)
    {
        [self touchBtnAtIndex:item.tag];
    }
}

-(void)touchBtnAtIndex:(NSInteger)index
{
    NSLog(@"登陆ssss");
    if(index!=0){
    LoginMainViewController *loginmain =[[LoginMainViewController alloc] initWithNibName:nil bundle:nil];
    loginmain.TypeLoginMain = @"0";
        [self.navigationController pushViewController:loginmain animated:YES];}
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField endEditing:YES];
    return YES;
}



@end







 